package framework.service.impl;

import framework.dao.RecordDao;
import framework.dao.imp.RecordDaoImp;
import framework.pojo.Page;
import framework.pojo.Record;
import framework.service.RecordService;

import java.util.List;

public class RecordServiceImp implements RecordService {

    RecordDao recordDao = new RecordDaoImp();

    @Override
    public int AddRecord(Record entry) {
        return recordDao.AddRecord(entry);
    }

    @Override
    public Page GetRecord(Integer uid, Boolean stat, Integer page_id, Integer pageSize) {

        Page<Record> page =new Page<>();

        //获取符合条件的条目数
        Integer entry_count= recordDao.GetEntryCount(uid,stat);
        Integer page_num;

        //根据entry_count和page_size计算page_num
        if(entry_count%pageSize>0)
            page_num=entry_count/pageSize+1;
        else page_num=entry_count/pageSize;

        //如果跳转超过最大页数，返回最后一页
        if(page_num!=0&&page_id>page_num)
            page_id=page_num;

        //获取符合条件的记录列表
        List<Record> items= recordDao.GetRecordByUidAndStat(uid,stat,page_id,pageSize);

        // 设置每页显示的数量
        page.setPageSize(pageSize);

        // 设置总记录数
        page.setPageTotalCount(entry_count);

        // 设置总页码
        page.setPageTotal(page_num);

        // 设置当前页码
        page.setPageNo(page_id);

        // 设置当前页数据
        page.setItems(items);

        return page;
    }
}
